const app = getApp()
import Pay from '../../utils/pay'
Page({
  data: {
    active: -1,
    status: [
      { tit: '全部订单', id: -1 },
      { tit: '待付款', id: 0 },
      { tit: '待使用', id: 1 },
      { tit: '待评价', id: 2 },
      { tit: '已完成', id: 3 },
    ],
    imgurl: app.globalData.imgurl,
    show: false,
    pay_method: 1,
    page: 1,
    list: [],
  },
  onLoad({ status }) {
    this.setData({
      active: status - 0,
    })
    this._getOrderList(status)
  },
  onChangeTab({ detail: { name } }) {
    this.setData({
      active: name,
      page: 1,
      list: [],
    })
    this._getOrderList(name)
  },
  async _getOrderList(name) {
    const { page, list } = this.data
    let { code, data } = await wx.requestAsync({
      url: '/order',
      data: {
        page,
        limit: 10,
        status: name == 0 ? 0 : name || -1,
      },
    })
    if (code == 0) {
      this.setData({
        list: [...list, ...data],
      })
    }
  },
  // 去往门店
  _toStore({
    currentTarget: {
      dataset: { id },
    },
  }) {
    wx.navigateTo({
      url: '/pages/storeIntroduction/storeIntroduction?s_id=' + id,
    })
  },
  // 点击付款
  async _toCash({
    currentTarget: {
      dataset: { oid },
    },
  }) {
    let { code, data } = await wx.requestAsync({
      url: '/order/token',
    })
    if (code == 0) {
      this.setData({
        token: data,
        show: true,
        o_id: oid,
      })
    }
  },
  // 选择支付方式
  _success({ detail }) {
    this.setData({
      pay_method: detail,
      show: false,
    })
    this._pay()
  },
  // 支付
  async _pay() {
    let { o_id, token, pay_method, active } = this.data
    let { code, data } = await wx.requestAsync({
      url: '/order/pay',
      data: { o_id, token, pay_method },
    })
    if (code == 0) {
      if (!data) {
        wx.showToast({
          title: '支付成功',
          icon: 'none',
          duration: 2000,
          success: () => {
            setTimeout(() => {
              this._getOrderList(active)
            }, 1000)
          },
        })
      } else {
        // 调用微信支付接口
        Pay({ ...data }, data)
        this._getOrderList(active)
      }
    }
  },
  // 使用 & 取消
  async _toCode({
    currentTarget: {
      dataset: { status, id },
    },
  }) {
    // 使用
    if (status == 1) {
      wx.navigateTo({
        url: '/pages/code/code?o_id=' + id,
      })
    }
    //取消订单
    else if (status == 0) {
      let { code } = await wx.requestAsync({
        url: '/order/cancel',
        data: {
          o_id: id,
        },
      })
      if (code == 0) {
        let { active } = this.data
        this._getOrderList(active)
        return wx.showToast({
          title: '取消订单成功',
          icon: 'none',
          duration: 2000,
        })
      }
    }
  },
  // 去评价
  _toevaluation({
    currentTarget: {
      dataset: { oid },
    },
  }) {
    wx.navigateTo({
      url: '/pages/evaluation/evaluation?o_id=' + oid,
    })
  },
  // 去详情
  _toorderdetails({
    currentTarget: {
      dataset: { oid, sid },
    },
  }) {
    wx.navigateTo({
      url: `/pages/orderdetails/orderdetails?o_id=${oid}&s_id=${sid}`,
    })
  },
  // 上拉加载
  onReachBottom() {
    let { page } = this.data
    this.setData({ page: page + 1 })
    this._getOrderList()
  },
})
